package com.gk.study.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gk.study.Vo.ChatListVO;
import com.gk.study.entity.ChatMessage;
import com.gk.study.service.ChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/messages")
public class ChatController {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private ChatMessageService chatMessageService;

    @MessageMapping("/chat")
    public void sendMessage(@Payload ChatMessage message) {
        // 1. 保存消息到数据库
        chatMessageService.saveMessage(message);

        // 2. 转发消息给接收者（使用用户ID路由）
        messagingTemplate.convertAndSendToUser(
                message.getReceiverId(),
                "/queue/messages",
                message
        );
    }

    @GetMapping
    public List<ChatMessage> getHistory(
            @RequestParam("currentUserId") String currentUserId,
            @RequestParam("targetUserId") String targetUserId
    ) {
        return chatMessageService.getChatHistory(currentUserId, targetUserId);
    }

    @GetMapping("/chat-list")
    public List<ChatListVO> chatList(@RequestParam String userId) {
        return chatMessageService.getChatList(userId);
    }




}